<?php
/**
 * Created by PhpStorm.
 * User: liujun
 * Date: 2018/8/10
 * Time: 下午2:31
 */

namespace PddWechat\Models\Pddorder\Dao;
use Dai\Framework\Base\BaseDao;
use Dai\Framework\Library\Log;
use PddWechat\Models\Pddorder\DataObject\PddOrderDo;


/**
 * Class PddOrderDao
 * @package PddWechat\Models\Pddorder\Dao
 * @dataObject(\PddWechat\Models\Pddorder\DataObject\PddOrderDo)
 */
class PddOrderDao extends BaseDao
{
    /**
     * @param PddOrderDo $pddOrderDo
     * @return PddOrderDo
     */
    public function insert( PddOrderDo $pddOrderDo )
    {
        var_dump($pddOrderDo);
        $pddOrderDo->id =  $this->execute("order.pdd_order.insert", $pddOrderDo);
        return $pddOrderDo;
    }


    /**
     * @param $orderSn
     * @return object
     */
    public function selectByOrderId( $orderSn )
    {
        $param = [
            'orderSn' => $orderSn,
            'select' => '*',
        ];
        $res = $this->execute("order.pdd_order.select_by_order_sn", $param);
        if( count($res) > 0 ){
            return $this->trimBase($res[0]);
        }else {
            return null;
        }
    }

    public function edit( PddOrderDo $pddOrderDo, $where=null){
        if( trim($where) == "" && intval($pddOrderDo->id) <= 0) {
            Log::error("update where or id must exist");
            return false;
        }
        $pddOrderDo->updateTime =  date('Y-m-d H:i:s', time());
        $param = [
            'update' =>  json_decode(json_encode($pddOrderDo), true),
            'where' => (trim($where) == "") ? ' id = '. $pddOrderDo->id : $where,
        ];
        Log::info("edit PddOrderDao ,param is ".var_export($param, true));
        return $this->execute("order.pdd_order.edit", $param);
    }

    public function totalObj( PddOrderDo $where=null){
        $param = [
            'select' => 'COUNT(*) AS cnt',
        ];

        if( $where != null) {
            $data = json_decode(json_encode($where), true);
            $wheres = [];
            foreach ($data as $key => $value) {
                if( $value != null) {
                    $wheres[] = "$key='$value'";
                }
            }
            if(count($wheres) > 0) {
                $param['where'] = implode(" AND ", $wheres);
            }
        }

        Log::info("edit PddOrderDao ,param is ".var_export($param, true));
        $ret =  $this->execute("order.pdd_order.select", $param);
        return intval($ret[0]['cnt']);
    }

    public function selectList(PddOrderDo $pddOrderDo=null, $orderBy=""){
        $param = [
            'select' => '*',
        ];

        if( $pddOrderDo != null) {
            $data = json_decode(json_encode($pddOrderDo), true);
            $wheres = [];
            foreach ($data as $key => $value) {
                if( $value != null) {
                    $wheres[] = "$key='$value'";
                }
            }
            if(count($wheres) > 0) {
                $param['where'] = implode(" AND ", $wheres);
            }
        }

        if($orderBy != null) {
            $param['orderBy'] = $orderBy;
        }

        $res = $this->execute("order.pdd_order.select", $param);
        $output = [];
        if( count($res) > 0 ) {
            foreach ($res as $item) {
                $output[] = $this->trimBase($item);
            }
        }
        return $output;
    }

    public function selectOwnList($uid, $params) {
        if(empty($uid)) {
            return [];
        }
        $sql = "SELECT * FROM jd.pdd_order WHERE uid = " . intval($uid);
        if($params->orderStatus !== null) {
            $sql .= " AND orderStatus = " . intval($params->orderStatus);
        }
        $sql .= " ORDER BY id DESC LIMIT ".($params->pn-1) * $params->rn . "," . $params->rn;
        $res = $this->queryRaw($sql);
        $output = [];
        if( count($res) > 0 ) {
            foreach ($res as $item) {
                $output[] = $this->trimBase($item);
            }
        }
        return $output;
    }

    public function selectOwnSpreadList($uid,$params) {
        if(empty($uid)) {
            return [];
        }
        $sql = "SELECT * FROM jd.pdd_order WHERE inviteUid = " . intval($uid);
        if($params->orderStatus !== null) {
            $sql .= " AND orderStatus = " . intval($params->orderStatus);
        }
        $sql .= " ORDER BY id DESC LIMIT ".($params->pn-1) * $params->rn . "," . $params->rn;
        $res = $this->queryRaw($sql);
        $output = [];
        if( count($res) > 0 ) {
            foreach ($res as $item) {
                $output[] = $this->trimBase($item);
            }
        }
        return $output;
    }

    public function selectAllSpreadList($uid,$params) {
        if(empty($uid)) {
            return [];
        }
        $sql = "SELECT * FROM jd.pdd_order WHERE (inviteUid = " . intval($uid) . " OR uid = " . intval($uid)
        . " OR inviteParentUid = " . intval($uid) . ")";
        if($params->orderStatus !== null) {
            $sql .= " AND orderStatus = " . intval($params->orderStatus);
        }
        $sql .= " ORDER BY id DESC LIMIT ".($params->pn-1) * $params->rn . "," . $params->rn;
        $res = $this->queryRaw($sql);
        $output = [];
        if( count($res) > 0 ) {
            foreach ($res as $item) {
                $output[] = $this->trimBase($item);
            }
        }
        return $output;
    }

    public function selectIncome($uid,$beginTime,$endTime) {
        if(empty($uid)) {
            return 0;
        }
        $sql = "SELECT SUM(uidMoney) AS uidMoney FROM jd.pdd_order WHERE uid = " . intval($uid) ." AND createTime BETWEEN 
        '" . $beginTime . "' AND '" . $endTime . "'";
        $uidMoney = intval($this->queryRaw($sql)[0]["uidMoney"]);

        $sql = "SELECT SUM(inviteUidMoney) AS inviteUidMoney FROM jd.pdd_order WHERE inviteUid = " . intval($uid) ." AND createTime BETWEEN 
        '" . $beginTime . "' AND '" . $endTime . "'";
        $inviteUidMoney = intval($this->queryRaw($sql)[0]["inviteUidMoney"]);

        $sql = "SELECT SUM(inviteParentUidMoney) AS inviteParentUidMoney FROM jd.pdd_order WHERE inviteParentUid = " . intval($uid) ." AND createTime BETWEEN 
        '" . $beginTime . "' AND '" . $endTime . "'";
        $inviteParentUidMoney = intval($this->queryRaw($sql)[0]["inviteParentUidMoney"]);
        return $uidMoney + $inviteUidMoney + $inviteParentUidMoney;
    }

    public function selectDirectUserIncome($uid) {
        if(empty($uid)) {
            return 0;
        }
        $sql = "SELECT SUM(uidMoney) as uidMoney FROM jd.pdd_order WHERE uid = ".$uid;
        $uidMoney = intval($this->queryRaw($sql)[0]["uidMoney"]);
        $sql = "SELECT SUM(inviteUidMoney) as inviteUidMoney FROM jd.pdd_order WHERE inviteUid = ".$uid;
        $inviteUidMoney = intval($this->queryRaw($sql)[0]["inviteUidMoney"]);
        return $uidMoney + $inviteUidMoney;
    }

    public function selectByOrderStatus($orderStatus) {

        $sql = "SELECT * FROM jd.pdd_order WHERE orderStatus = " . intval($orderStatus) . " AND flag = 0";
        $res = $this->queryRaw($sql);
        $output = [];
        if( count($res) > 0 ) {
            foreach ($res as $item) {
                $output[] = $this->trimBase($item);
            }
        }
        return $output;
    }

    public function updateOrderStatusByOrderId($orderId) {
        if(empty($orderId)) {
            return 0;
        }
        $sql = "UPDATE jd.pdd_order SET flag = 1 WHERE id = " . intval($orderId);
        $res = $this->execRaw($sql);
        return $res;
    }
}